package ferri.lorenzo;



import android.app.ListActivity;
import android.content.Context;

import android.content.Intent;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.*;


public class SearchSong extends ListActivity{
	
	String path;
	Intent dat = new Intent();
    private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private Bitmap[] mIcon= new Bitmap[4];
        
 
        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            // Icons bound to the rows.
            mIcon[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_album);
            mIcon[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.guitar);
            mIcon[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_15488);
            mIcon[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_radio);
        }

        /**
         * The number of items in the list is determined by the number of speeches
         * in our array.
         *
         * @see android.widget.ListAdapter#getCount()
         */
        public int getCount() {
            return DATA.length;
        }

        /**
         * Since the data comes from an array, just returning the index is
         * sufficent to get at the data. If we were using a more complex data
         * structure, we would return whatever object represents one row in the
         * list.
         *
         * @see android.widget.ListAdapter#getItem(int)
         */
        public Object getItem(int position) {
            return position;
        }

        /**
         * Use the array index as a unique id.
         *
         * @see android.widget.ListAdapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }

        /**
         * Make a view to hold each row.
         *
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(DATA[position]);
            
            holder.icon.setImageBitmap(mIcon[position]);

            return convertView;
        }

        static class ViewHolder {
            TextView text;
            ImageView icon;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        setListAdapter(new EfficientAdapter(this));
        Button b = (Button)findViewById(R.id.button_ok);
        b.setText("Vai alla riproduzione");
       
      
      
		
    }

    private static final String[] DATA = {
            "Album", "Artisti", "Canzoni",
            "Playlist"};
    
    
	protected void onListItemClick(ListView parent, View v,
			int position, long id) {
		switch (position)
		{
		case 0:
			Intent i = new Intent(this, AlbumListActivity.class);
			startActivityForResult(i,0);
			break;
		case 1:
			i = new Intent(this, ArtistListActivity.class);
			startActivityForResult(i,0);
			break;
		case 2:
			i = new Intent(this, SongListActivity.class);
			startActivityForResult(i, 0);
			break;
		case 3:
			break;
		default:
				break;

		
		} 
		
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
		Intent data) {
        // You can use the requestCode to select between multiple child
        // activities you may have started.  Here there is only one thing
        // we launch.
        if (requestCode == 0) {

            // We will be adding to our text.
        	

            // This is a standard resultCode that is sent back if the
            // activity doesn't supply an explicit result.  It will also
            // be returned if the activity failed to launch.
            if (resultCode == RESULT_CANCELED) {
                //texts =("(cancelled)");

            // Our protocol with the sending activity is that it will send
            // text in 'data' as its result.
            } else {
               
                if (data != null) {
                	dat=data;
        		//	Log.d("response", data.getStringExtra("io"));
                }
            }

         
        }
    }
	
	@Override
	protected void onStart(){
		super.onStart();

	}
	
	 public void OK(View view) {

			switch (view.getId()) {
			case R.id.button_ok:
				
			   
				
				 setResult(RESULT_OK, dat);
		         finish();
				
			}
	}
   
	
}
